#include <iostream>
#include <pthread.h>
#include <unistd.h>


pthread_cond_t cond;
pthread_mutex_t mutex;
void *printA(void *arg)
{

    while (1)
    {
        pthread_cond_wait(&cond, &mutex);
        std::cout << "我是线程A" << std::endl;
        sleep(1);
        pthread_cond_signal(&cond);
    }
}
void *printB(void *arg)
{
    while (1)
    {
        std::cout << "我是线程B" << std::endl;
        sleep(1);
        pthread_cond_signal(&cond);
        pthread_cond_wait(&cond, &mutex);
    }
}

int main()
{

    pthread_t Aprthead, Bprthead;
    pthread_cond_init(&cond, NULL);
    pthread_mutex_init(&mutex, NULL);

    pthread_create(&Aprthead, NULL, printA, NULL);
    pthread_create(&Bprthead, NULL, printB, NULL);

    pthread_join(Aprthead, NULL);
    pthread_join(Bprthead, NULL);

    pthread_cond_destroy(&cond);
    pthread_mutex_destroy(&mutex);

    return 0;
}